package com.zeus.rpc;

import com.alibaba.fastjson.JSON;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * com.zeus.rpc
 * Created by zengxm on 2016/4/23 0023.
 */
public class TerminalClient {
	private TerminalService.Client client;

	private TTransport transport;

	@Before
	public void init() throws TTransportException {
		transport = new TFramedTransport(new TSocket(TerminalServer.host,TerminalServer.port));
		//transport = new THttpClient("http://"+host+":"+port);
		transport.open();

		TProtocol protocol = new TCompactProtocol(transport);
		client = new TerminalService.Client(protocol);
	}

	public TerminalRequest createRequest(){
		TerminalRequest req=new TerminalRequest();

		return req;
	}

	@Test
	public void request() throws TException {
		System.out.println(JSON.toJSONString(client.action(createRequest())));

		System.out.println(JSON.toJSONString(client.status(createRequest())));

		System.out.println(JSON.toJSONString(client.shutdown(createRequest())));
	}

	@After
	public void close(){
		if(transport != null && transport.isOpen()){
			try{
				transport.close();
			}catch (Exception e){
				e.printStackTrace();
			}
		}
	}
}
